package com.stone.bigdata.cron;

import com.stone.bigdata.dao.fota.ProductListDao;
import com.stone.bigdata.dao.poc.ActiveCityDao;
import com.stone.bigdata.domain.AddCount;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.sql.Date;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

public class AfterFiveMinAdd extends QuartzJobBean {
    private ProductListDao productListDao;

    private ActiveCityDao activeCityDao;

    @Autowired
    public AfterFiveMinAdd(ProductListDao productListDao, ActiveCityDao activeCityDao) {
        this.productListDao = productListDao;
        this.activeCityDao = activeCityDao;
    }

    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext)
            throws JobExecutionException {
        System.out.println("统计分时总数");
        LocalDateTime now = LocalDateTime.now();
        // 今天00:00:00
        Timestamp todayZero = Timestamp.valueOf(LocalDateTime.of(now.toLocalDate(), LocalTime.MIN));
        Timestamp fiveStamp = Timestamp.valueOf(LocalDateTime.of(now.toLocalDate(), LocalTime.of(now.getHour(), now.getMinute(), 0)));
        Integer total = productListDao.getTotalAdd(todayZero, fiveStamp);
        if (total != null) {
            AddCount addCount = new AddCount();
            addCount.setTotal(total);
            addCount.setCreateTime(fiveStamp);
            activeCityDao.saveFiveAfter(addCount);
        }
    }
}
